package com.xiguanlezz.cn.common.utils;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/11/28 13:50
 * @Description：
 */
public class PropertyUtils {

    private static final String PROCESSORS_ENV_NAME = "NACOS_COMMON_PROCESSORS";
    private static final String PROCESSORS_PROP_NAME = "nacos.common.processors";


    private PropertyUtils() {
    }

    public static int getProcessorsCount() {
        int processorsCount = 0;
        String processorsCountPreSet = getProperty(PROCESSORS_PROP_NAME, PROCESSORS_ENV_NAME);
        if (processorsCountPreSet != null) {
            try {
                processorsCount = Integer.parseInt(processorsCountPreSet);
            } catch (NumberFormatException ignored) {
            }
        }
        if (processorsCount <= 0) {
            processorsCount = Runtime.getRuntime().availableProcessors();
        }
        return processorsCount;
    }

    public static String getProperty(String propertyName, String envName) {
        return System.getenv().getOrDefault(envName, System.getProperty(propertyName));
    }

}
